package com.baichang.taoshunda.dao.impl;

import org.springframework.stereotype.Repository;

import com.baichang.framework.dao.impl.BaseDaoImpl;
import com.baichang.taoshunda.dao.IdleGoodsDao;
import com.baichang.taoshunda.entity.IdleGoods;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class IdleGoodsDaoImpl extends BaseDaoImpl<IdleGoods> implements IdleGoodsDao {

    /**
     * 查询闲置物品
     *
     * @param userId
     * @param cityId
     * @param name
     * @param nowPage
     * @param pageSize @return
     */
    @Override
    public List<Map<String, Object>> queryIdleGoods(Integer userId, Integer cityId, Integer areaId, String name, int nowPage, int pageSize) {
        List list = new ArrayList();
        String sql = "SELECT g.* FROM {table} g" +
                " INNER JOIN t_user u ON user_id=u.id AND user_state=1 WHERE status=1 ";
        if (!name.equals("")) {
            sql += " AND name LIKE ? ";
            list.add("%" + name + "%");
        }
        if (userId > 0) {
            sql += " AND user_id=? ";
            list.add(userId);
        }
        if (cityId > 0) {
            sql += " AND city_id=? ";
            list.add(cityId);
        }
        if (areaId > 0) {
            sql += " AND area_id=? ";
            list.add(areaId);
        }
        sql += " ORDER BY created_time DESC ";
        return listForMapList(sql, nowPage, pageSize, list.toArray());
    }

    /**
     * 查询闲置详情
     *
     * @param idleId
     * @return
     */
    @Override
    public Map<String, Object> queryIdleGoodsDetails(Integer idleId) {
        String sql = "SELECT * FROM {table} WHERE status=1 AND id=? ";
        sql += " ORDER BY created_time DESC ";
        return queryForMap(sql, idleId);
    }

    /**
     * 删除闲置商品
     *
     * @param idleId
     * @return
     */
    @Override
    public int deleteIdle(Integer idleId) {
        String sql = "UPDATE {table} SET status=0 WHERE id=?";
        return update(sql, idleId);
    }

    /**
     * 闲置商品分享
     *
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> getIdleGoodsById(Integer id) {
        String sql = "SELECT * FROM {table} WHERE id=? AND status=1";
        return queryForMap(sql, id);
    }

    /**
     * 查询收藏的闲置商品
     *
     * @param userId   用户编号
     * @param nowPage
     * @param pageSize @return
     * @return
     */
    @Override
    public List<Map<String, Object>> findIdleCollection(Integer userId, Integer nowPage, int pageSize) {
        String sql = "SELECT ic.id, ig.id AS goodsId, ig.`name`, ig.old_price AS oldPrice, ig.new_price AS newPrice, " +
                "ig.idle_pic AS idlePic, ig.created_time AS createdTime, ig.sunmmary, ig.address, ig.`status` , ig.city_id AS cityId, " +
                "ig.area_id AS areaId, ig.user_id AS userId FROM t_idle_collection ic INNER JOIN t_idle_goods ig ON ig.id = ic.idle_goods_id WHERE ic.user_id = ? AND ig.status=1";
        return listForMapList(sql, nowPage, pageSize, userId);
    }
}